package com.telecom.controller.user;

import com.telecom.common.context.UserContext;
import com.telecom.common.convention.result.PageDTO;
import com.telecom.common.convention.result.Result;
import com.telecom.pojo.dto.user.order.OrderCreateRequestDTO;
import com.telecom.pojo.dto.user.order.OrderPageQueryRequestDTO;
import com.telecom.pojo.dto.user.order.OrderPaymentRequestDTO;
import com.telecom.pojo.dto.user.order.OrderRatingRequestDTO;
import com.telecom.pojo.vo.user.order.OrderDetailVO;
import com.telecom.pojo.vo.user.order.OrderItemVO;
import com.telecom.pojo.vo.user.order.OrderPickupCodeVO;
import com.telecom.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;

/**
 * 用户订单相关接口
 */
@RestController
@RequestMapping("/user/order")
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 根据mealId下单
     * 
     * @param createRequest 创建订单请求参数
     * @return 创建的订单ID
     */
    @PostMapping
    public Result<Long> createOrder(@RequestBody @Valid OrderCreateRequestDTO createRequest) {
        //获取套餐id
        Long mealId = createRequest.getMealId();

        //获取用户id
        Long userID = UserContext.getUser();

        //创建订单
        Long orderId = orderService.createOrder(mealId,userID);
        return Result.success(orderId);
    }

    /**
     * 取消订单
     *
     * @param id 订单ID
     * @return 取消结果
     */
    @PutMapping("/cancel/{id}")
    public Result<Void> cancel(@PathVariable @Positive(message = "订单ID必须为正数") Long id) {
        //取消订单
        orderService.userCancelOrder(id);
        return Result.success();
    }


    /**
     * 支付订单
     *
     * @param paymentRequest 支付请求参数
     * @return 支付结果
     */
    @PutMapping("/pay")
    public Result<Void> pay(@RequestBody @Valid OrderPaymentRequestDTO paymentRequest) {
        orderService.updateOrderPayStatus(paymentRequest);
        return Result.success();
    }


    /**
     * 根据订单id评价是否满意
     *
     * @param ratingRequest 评价参数
     * @return 评价结果
     */
    @PutMapping("/rating")
    public Result<Void> rating(@RequestBody @Valid OrderRatingRequestDTO ratingRequest) {
        long id = ratingRequest.getId();
        boolean isSatisfied = ratingRequest.getIsSatisfied();
        orderService.rating(id, isSatisfied);
        return Result.success();
    }


    /**
     * 分页条件查询订单
     * 
     * @param queryRequest 查询条件参数
     * @return 订单列表分页结果
     */
    @GetMapping("/page")
    public Result<PageDTO<OrderItemVO>> page(@Valid OrderPageQueryRequestDTO queryRequest) {
        log.info("用户订单分页查询:{}", queryRequest);
        PageDTO<OrderItemVO> pageResult = orderService.userPageQuery(queryRequest);
        return Result.success(pageResult);
    }


    /**
     * 根据id获取取餐码
     * 
     * @param id 订单ID
     * @return 取餐码信息
     */
    @GetMapping("/code/{id}")
    public Result<OrderPickupCodeVO> getCodeById(@PathVariable @Positive(message = "订单ID必须为正数") Long id) {
        log.info("查询订单的取餐码：{}", id);
        OrderPickupCodeVO orderPickupCodeVO = orderService.getOrderPickupCode(id);
        return Result.success(orderPickupCodeVO);
    }

    /**
     * 获取订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public Result<OrderDetailVO> getOrderDetails(@PathVariable @Positive(message = "订单ID必须为正数") Long id){
        log.info("查询订单详情，订单ID：{}", id);
        OrderDetailVO orderDetails = orderService.getUserOrderDetails(id);
        return Result.success(orderDetails);
    }

}